#include "imagelistdisplay.h"

ImageListDisplay::ImageListDisplay()
{
    currentImage = 0;
    imageSize = QSize(128, 96);

    botLayout = new QHBoxLayout();
    prevBut = new QPushButton("Precedent");
    nextBut = new QPushButton("Suivant");
    imageOne = new QLabel();
    imageTwo = new QLabel();
    imageThree = new QLabel();
    imageFour = new QLabel();
    imageFive = new QLabel();
    imageOne->setMaximumWidth(128);
    imageTwo->setMaximumWidth(128);
    imageThree->setMaximumWidth(128);
    imageFour->setMaximumWidth(128);
    imageFive->setMaximumWidth(128);
    this->setLayout(botLayout);
    botLayout->addWidget(prevBut);
    botLayout->addWidget(imageOne);
    botLayout->addWidget(imageTwo);
    botLayout->addWidget(imageThree);
    botLayout->addWidget(imageFour);
    botLayout->addWidget(imageFive);
    botLayout->addWidget(nextBut);

    imageOne->setPixmap(QPixmap::fromImage(QImage("image/image.png")).scaled(imageSize));
    imageTwo->setPixmap(QPixmap::fromImage(QImage("image/image.png")).scaled(imageSize));
    imageThree->setPixmap(QPixmap::fromImage(QImage("image/image.png")).scaled(imageSize));
    imageFour->setPixmap(QPixmap::fromImage(QImage("image/image.png")).scaled(imageSize));
    imageFive->setPixmap(QPixmap::fromImage(QImage("image/image.png")).scaled(imageSize));

    connect(prevBut, SIGNAL(clicked()), this, SIGNAL(previmg()));
    connect(nextBut, SIGNAL(clicked()), this, SIGNAL(nextimg()));

    imageOne->installEventFilter(this);
    imageTwo->installEventFilter(this);
    imageThree->installEventFilter(this);
    imageFour->installEventFilter(this);
    imageFive->installEventFilter(this);

    prevBut->setEnabled(false);
    nextBut->setEnabled(false);
}

bool ImageListDisplay::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(obj == imageOne)
        {
            emit pressImage(-2 + currentImage);
            return true;
        }
        else if(obj == imageTwo && listSize>1)
        {
            emit pressImage(-1 + currentImage);
            return true;
        }
        else if(obj == imageThree && listSize>2)
        {
            emit pressImage(currentImage);
            return true;
        }
        else if(obj == imageFour && listSize>3)
        {
            emit pressImage(1 + currentImage);
            return true;
        }
        else if(obj == imageFive && listSize>4)
        {
            emit pressImage(2 + currentImage);
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

void ImageListDisplay::loadImages(QPixmap img1, QPixmap img2, QPixmap img3, QPixmap img4, QPixmap img5, int size, int pos)
{
    listSize=size;
    currentImage = pos;
    imageOne->setPixmap(img1.scaled(imageSize));
    imageTwo->setPixmap(img2.scaled(imageSize));
    imageThree->setPixmap(img3.scaled(imageSize));
    imageFour->setPixmap(img4.scaled(imageSize));
    imageFive->setPixmap(img5.scaled(imageSize));
    switch(currentImage)
    {
    case -2:
        imageOne->setStyleSheet("border: 1px solid white;");
        imageTwo->setStyleSheet("border: 1px solid white;");
        imageThree->setStyleSheet("border: 1px solid white;");
        imageFour->setStyleSheet("border: 1px solid white;");
        imageFive->setStyleSheet("border: 1px solid black;");
        break;
    case -1:
        imageOne->setStyleSheet("border: 1px solid white;");
        imageTwo->setStyleSheet("border: 1px solid white;");
        imageThree->setStyleSheet("border: 1px solid white;");
        imageFour->setStyleSheet("border: 1px solid black;");
        imageFive->setStyleSheet("border: 1px solid white;");
        break;
    case 0:
        imageOne->setStyleSheet("border: 1px solid white;");
        imageTwo->setStyleSheet("border: 1px solid white;");
        imageThree->setStyleSheet("border: 1px solid black;");
        imageFour->setStyleSheet("border: 1px solid white;");
        imageFive->setStyleSheet("border: 1px solid white;");
        break;
    case 1:
        imageOne->setStyleSheet("border: 1px solid white;");
        imageTwo->setStyleSheet("border: 1px solid black;");
        imageThree->setStyleSheet("border: 1px solid white;");
        imageFour->setStyleSheet("border: 1px solid white;");
        imageFive->setStyleSheet("border: 1px solid white;");
        break;
    case 2:
        imageOne->setStyleSheet("border: 1px solid black;");
        imageTwo->setStyleSheet("border: 1px solid white;");
        imageThree->setStyleSheet("border: 1px solid white;");
        imageFour->setStyleSheet("border: 1px solid white;");
        imageFive->setStyleSheet("border: 1px solid white;");
        break;
    }
}

void ImageListDisplay::setPrevEnabled(bool enabled)
{
    prevBut->setEnabled(enabled);
}

void ImageListDisplay::setNextEnabled(bool enabled)
{
    nextBut->setEnabled(enabled);
}

bool ImageListDisplay::prevEnabled()
{
    return prevBut->isEnabled();
}

bool ImageListDisplay::nextEnabled()
{
    return nextBut->isEnabled();
}
